package Compare;


import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @Program: CompareFiles
 * @Description:  先判断长度是否相同，再判断内容是否相同，最后通过MD5和SHA-1来判断是否相同
 * @Author: zqs
 * @Create： 2021-07-14-12:10
 */

/**
 * 计算文件的 MD5 值 根据MD5值 判断文件是否是同一个文件
 * @return String
 */
public class CompareFile {
    public static String getMd5(File file1){
        FileInputStream file = null;
        if (!file1.isFile()) {
            return null;
        }
        try {
            file = new FileInputStream(file1);
            String md5hex = DigestUtils.md5Hex(file);
            return md5hex;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "错误！";
    }
    @Deprecated
    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            //使用这个方法，会导致0开头的MD5值不会显示0，使用DigestUtils工具包获取到的MD5值正常
            return bigInt.toString(16);
//            return SystemHWUtil.toHexString(digest.digest()); 。
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 计算文件的 SHA-1 值 根据SHA-1值 判断文件是否是同一个文件
     * @param file
     * @return String
     */
    public static String getFileSha1(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest = MessageDigest.getInstance("SHA-1");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            return bigInt.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断内容是否相同
     * @param fileA
     * @param fileB
     * @return
     * @throws IOException
     */
    public static String content(File fileA,File fileB) throws IOException {
        FileInputStream File1 = null;
        FileInputStream File2 = null;
        if (!fileA.isFile()||!fileB.isFile()) {
            throw new IOException("NOT FILES！");
        }
        try {
            File1 = new FileInputStream(fileA);
            File2 = new FileInputStream(fileB);

            try {

                if (File1.available() != File2.available()) {
                    //长度不同内容肯定不同
                  return fileA.getName()+ "\n IS NOT EQUAL TO \n" + fileB.getName();
                } else {
                    boolean tag = true;
                    while (File1.read() != -1 && File2.read() != -1) {
                        if (File1.read() != File2.read()) {
                            tag = false;
                            break;
                        }
                    }
                    if (tag == true) {
                        return  fileA + "\n EQUALS TO \n" + fileB;
                    } else {
                        return fileA.getName() + "\n IS NOT equal to \n" + fileB.getName();
                    }
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File can't find..");
        } finally {

            try {
                if (File1 != null) {
                    File1.close();
                }
                if (File2 != null) {
                    File2.close();
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
        return null;
    }

}
